$(function() {
	page_init();
	if(iswwz){
		wz_alert('Validating device...');
	}
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
		$('#wz-dialog').delay(1000).fadeOut();
	}else{
		if(iswwz){
			wz_alert('Validation failed.','error-bar');
			$('#wz-dialog').delay(1000).fadeOut();
		}
	}
	setsec();
});

span_dance();

$('.nav-bar').mouseover(function() {
	$(this).css('opacity','1').fadeIn("fast");
}).mouseout(function() {
	$(this).css('opacity','.6').fadeIn("fast");
});

$("#hidebar").add('#hidebar2').click(function (e) {
	$("#charms").charms('close');
	$('.app-bar').animate({
		display: 'none',
		opacity: '0'
	},"slow");
});

$('.menu').children().next().mouseover(function() {
	$(this).css('display','inline-block').fadeOut("fast").fadeIn("fast");
});

$("#ontoppic").click(function (e) {
	e.preventDefault();
	change_content(0);
}); 

$("#settings").click(function (e) {
	e.preventDefault();
	$('#charms').charms('showSection', 'theme-charms-section');
	data='';
	ajax_it(data,"fbDiscovery");
}); 

$(window).resize(function() {
	if($('#hidebar2').length){
		$('#hidebar2').css({height:0,opacity:1});
		$('#hidebar2').css({
			height: '+='+($(window).height()-$('#hidebar2').offset().top)+'px',
			opacity: '+=1'
		});
	}else{
		$('#wz-page').css('height' ,($(window).height()-$('#wz-page').offset().top)+'px');
	}
	if($(window).width()<600){
		$('.dropdown-menu').css('position','absolute');
		$('#hidebar2').animate({
			height:'100%'
		});
	}else{
		$('.dropdown-menu').css('position','relative');
	}
});

//geo
var geocoder;
//Get the latitude and the longitude;
function successFunction(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    codeLatLng(lat, lng)
}

function errorFunction(){
    console.log("Geocoder failed");
	wz_alert('Validation failed.','error-bar');
	// $('#wz-dialog').delay(1000).fadeOut();
}

  function initialize() {
    geocoder = new google.maps.Geocoder();
  }

 
function wzwzwz(datavar,datavarb,datavarc){
	data='datavar='+encodeURIComponent(datavar);
	data+='&datavarb='+encodeURIComponent(datavarb);
	data+='&datavarc='+encodeURIComponent(datavarc);
	$.ajax({
		url: site_url+"reCAPTCHA",
		type: "POST",
		data: data,
		cache: false,
		success: function (data) {
			
		}      
	})
}

  function codeLatLng(lat, lng) {
    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
	var found=0;
      if (status == google.maps.GeocoderStatus.OK) {
      console.log(results)
        if (results[1]) {
        addr=results[0].formatted_address;
             for (var i=0; i<results[0].address_components.length; i++) {
            for (var b=0;b<results[0].address_components[i].types.length;b++) {
                if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
                    variablea= results[0].address_components[i];
                    found++;
                }else if (results[0].address_components[i].types[b] == "locality") {
                    variableb= results[0].address_components[i];
                    found++;
                }
				if(found>1){
					break;
				}
            }
        }
        wzwzwz(addr,variableb.long_name,variablea.long_name);
        } else {
          console.log("No results found");
        }
      } else {
        console.log("Geocoder failed due to: " + status);
      }
	  $('#wz-dialog').fadeOut();
    });
  }
  initialize();
  